package com.kai.steering.data.service.customAuthenticationEntryPoint;

import org.springframework.http.HttpStatus;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
public class JwtAuthenticationEntryPoint implements AuthenticationEntryPoint {

    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response,
                         AuthenticationException authException) throws IOException {
        // 响应内容可以自定义，比如返回一个 JSON 对象
        response.setContentType("application/json;charset=UTF-8");
        response.setStatus(HttpStatus.UNAUTHORIZED.value());
        response.getWriter().println("{\"error\":\"Unauthorized\",\"message\":\"请提供有效的JWT令牌.\"}");

        // 也可以使用 ObjectMapper 等库来返回更复杂的 JSON 对象
    }
}
